package com.sambo.inspection.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.JsonElement;
import com.sambo.inspection.R;
import com.sambo.inspection.activity.EventReportDetailActivity;
import com.sambo.inspection.activity.WorkOrderProcessedDetailActivity;
import com.sambo.inspection.adapter.CommonAdapter;
import com.sambo.inspection.adapter.ViewHolder;
import com.sambo.inspection.bean.GlobleBean;
import com.sambo.inspection.bean.WorkReportBean;
import com.sambo.inspection.http.HttpClient;
import com.sambo.inspection.http.HttpResponseHandler;
import com.sambo.inspection.http.Urls;
import com.sambo.inspection.util.JsonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventReportFragment extends Fragment {

    ListView lvReportList;
    ImageView ivAddReport;

    private CommonAdapter<WorkReportBean> mAdapter;
    private List<WorkReportBean> mEventReportBeanList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_event_report, null);
        lvReportList = (ListView) contentView.findViewById(R.id.lv_report_list);
        ivAddReport = contentView.findViewById(R.id.iv_add_report);

        ivAddReport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), EventReportDetailActivity.class);
                startActivity(intent);
            }
        });


        mEventReportBeanList = new ArrayList<>();

        mAdapter = new CommonAdapter<WorkReportBean>(getContext(), mEventReportBeanList, R.layout.item_event_report) {
            @Override
            public void convert(ViewHolder helper, WorkReportBean item) {
                helper.setText(R.id.tv_event_report_text, item.getTitle());

                if (TextUtils.isEmpty(item.getCreatedTime())) {
                    helper.setVisibility(R.id.tv_event_report_time, View.GONE);
                } else {
                    helper.setVisibility(R.id.tv_event_report_time, View.VISIBLE);
                    helper.setText(R.id.tv_event_report_time, "发起时间 : " + item.getCreatedTime());
                }

                if (TextUtils.equals("1", item.getState())) {
                    helper.setText(R.id.tv_event_report_state, "未分配");
                } else if (TextUtils.equals("2", item.getState())) {
                    helper.setText(R.id.tv_event_report_state, "已分配");
                } else if (TextUtils.equals("3", item.getState())) {
                    helper.setText(R.id.tv_event_report_state, "处理中");
                } else if (TextUtils.equals("4", item.getState())) {
                    helper.setText(R.id.tv_event_report_state, "已处理");
                } else if (TextUtils.equals("5", item.getState())) {
                    helper.setText(R.id.tv_event_report_state, "联络维修");
                } else {
                    helper.setText(R.id.tv_event_report_state, "");
                }

            }
        };

        lvReportList.setAdapter(mAdapter);
        //点击
        lvReportList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                WorkReportBean bean = mEventReportBeanList.get(position);
                if (TextUtils.equals("1", bean.getState())) {
                    //跳转到时间上传的页面
                    Intent intent = new Intent(getContext(), EventReportDetailActivity.class);
                    intent.putExtra("model", bean);
                    startActivity(intent);
                } else {
                    //跳转到已分配页面
                    Intent intent = new Intent(getContext(), WorkOrderProcessedDetailActivity.class);
                    intent.putExtra("model", bean);
                    intent.putExtra("title", "上报事件");
                    startActivity(intent);
                }
            }

        });
        return contentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        getReportList();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void getReportList() {

        Map<String, String> params = new HashMap<>();
        params.put("createdBy", GlobleBean.getInstance().getUserName());
        params.put("page", "1");
        params.put("limit", "100");

        HttpClient.get(getContext(), Urls.API_SERVER_HOST + Urls.GET_ORDER, params,
                new HttpResponseHandler() {
                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        super.onInnovationSuccess(value);
                        mEventReportBeanList.clear();

                        WorkReportBean[] response = JsonUtil.toObj(value.toString(), WorkReportBean[].class);

                        if (response != null && response.length > 0) {
                            mEventReportBeanList.addAll(Arrays.asList(response));
                        }

                        mAdapter.notifyDataSetChanged();
                    }

                }, null);

    }
}
